#R version 4.4.3 (2025-02-28 ucrt)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 11 x64 

library(dplyr) #1.1.4
library(ggplot2) #3.5.1
library(readr) #2.1.5
library(haven) #2.5.4
library(estimatr) #1.0.6
library(broom.mixed) #0.2.9.6
library(lme4) #1.1-37
library(countrycode) #1.6.1
library(forcats) #1.0.0
library(tibble) #3.2.1

`%notin%` <- function(x,y) !(x %in% y) 

setwd("C:/Users/selfd/Documents/autopartyinstitutionalization/legacy/cps/replication")

#read in data from Stata with PCSE----
#see Stata files for full replication
pi <- read_dta("data/stata/pi1_coefficients.dta") |> 
  select(estimate = c1, se = c2) |> 
  na.omit() |> 
  mutate(term = "Incumbent PI")|> 
  rownames_to_column(var = "baseline_year")

prior <- read_dta("data/stata/prior_pi_coefficients.dta") |> 
  select(estimate = c1, se = c2) |> 
  na.omit() |> 
  mutate(term = "Prior PI")|> 
  rownames_to_column(var = "baseline_year")

asp <- read_dta("data/stata/asp_dummy_coefficients.dta") |> 
  select(estimate = c1, se = c2) |> 
  na.omit() |> 
  mutate(term = "ASP")|> 
  rownames_to_column(var = "baseline_year")

bind_rows(prior, pi, asp) |> 
  mutate(conf.low = estimate - 1.96*se, conf.high = estimate + 1.96*se, baseline_year = as.numeric(baseline_year)) |> 
  ggplot(aes(x = as.factor(baseline_year), y = estimate, ymin = conf.low, ymax = conf.high, color = term, shape = term)) +
  geom_pointrange(show.legend = TRUE, position = position_jitterdodge(dodge.width = 0.85, jitter.width = 0.2), size = 0.5) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_line(aes(y = estimate - 100), linetype = "solid", color = NA) +  # Setting color to NA to remove legend
  geom_point(aes(y = estimate - 100), shape = NA, color = NA) +  # Setting color and shape to NA to remove legend
  theme_bw() +
  xlab("Years Since Democratization") +
  ylab("Party Institutionalization") +
  ylim(-0.55, 0.55) +
  scale_color_grey(name = "Variable", start = 0, end = 0.8) +
  scale_shape_manual("Variable", values = c(15, 16, 17)) + 
  theme(legend.position = "bottom") +
  guides(color = guide_legend(order = 1), shape = guide_legend(order = 1))

#save to file
#ggsave(filename = "C:/Users/selfd/Documents/autopartyinstitutionalization/legacy/plots/fig2_1.jpg", dpi = 500, width = 5, height = 5)

pi <- read_dta("data/stata/pi1_coefficients_noasp.dta") |> 
  select(estimate = c1, se = c2) |> 
  na.omit() |> 
  mutate(term = "Incumbent PI")|> 
  rownames_to_column(var = "baseline_year")

prior <- read_dta("data/stata/prior_pi_coefficients_noasp.dta") |> 
  select(estimate = c1, se = c2) |> 
  na.omit() |> 
  mutate(term = "Prior PI")|> 
  rownames_to_column(var = "baseline_year")

bind_rows(prior, pi) |> 
  mutate(conf.low = estimate - 1.96*se, conf.high = estimate + 1.96*se, baseline_year = as.numeric(baseline_year)) |> 
  ggplot(aes(x = as.factor(baseline_year), y = estimate, ymin = conf.low, ymax = conf.high, color = term, shape = term)) +
  geom_pointrange(show.legend = TRUE, position = position_jitterdodge(dodge.width = 0.85, jitter.width = 0.2), size = 0.5) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_line(aes(y = estimate - 100), linetype = "solid", color = NA) +  # Setting color to NA to remove legend
  geom_point(aes(y = estimate - 100), shape = NA, color = NA) +  # Setting color and shape to NA to remove legend
  theme_bw() +
  xlab("Years Since Democratization") +
  ylab("Party Institutionalization") +
  ylim(-0.55, 0.55) +
  scale_color_grey(name = "Variable", start = 0.5, end = 0.8) +
  scale_shape_manual("Variable", values = c(16, 17)) + 
  theme(legend.position = "bottom") +
  guides(color = guide_legend(order = 1), shape = guide_legend(order = 1))

#save to file
#ggsave(filename = "C:/Users/selfd/Documents/autopartyinstitutionalization/legacy/plots/fig2_2.jpg", dpi = 500, width = 5, height = 5)